/**
 * 
 */
package com.fly.sms.adapter;

import java.util.List;
import java.util.Map;

import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.Button;
import android.widget.TextView;

import com.fly.sms.CommonActivity;
import com.fly.sms.R;
import com.fly.sms.progress.ThreadProcess;
import com.fly.sms.service.CustomerContentService;

public class ContentpresetListAdapter extends IAdapter {
	private List<Map<String, Object>> list = null;

	public ContentpresetListAdapter(CommonActivity context,
			List<Map<String, Object>> list) {
		super(context);
		this.list = list;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	public View getView(final int position, View convertView, ViewGroup parent) {
		if (convertView == null) {
			convertView = context.getView(R.layout.contentpresetitem);
		}
		final Map<String, Object> map = list.get(position);
		TextView Name = (TextView) convertView.findViewById(R.id.Name);
		Button deleteBtn = (Button) convertView.findViewById(R.id.deleteBtn);
		WebView MsgContent=(WebView) convertView.findViewById(R.id.MsgContent);
		Name.setText(map.get("Name").toString());
		//MsgContent.setText(map.get("MsgContent").toString());
		MsgContent.loadDataWithBaseURL("", map.get("MsgContent")
				.toString(), "text/html", "utf-8", null);
		deleteBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				new ThreadProcess(context,new ThreadProcess.onProcess() {
					boolean flag=false;
					@Override
					public void onStart() {
						flag=CustomerContentService.delete(map.get("Id").toString());
					}
					
					@Override
					public void onFinished() {
						if(flag){
							context.showMsg(context.getText(R.string.oprsuccess));
							list.remove(position);
							ContentpresetListAdapter.this.notifyDataSetChanged();
						}else{
							context.showMsg(context.getText(R.string.oprfail));
						}
						
					}
				}).startProgress();
			}
		});
		return convertView;
	}
}
